﻿using System;
using System.Web.Http;
using yiCommerce.Core;
using yiCommerce.Core.Helper;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Messages;
using yiCommerce.Service.Products;

namespace yiCommerce.Areas.Admin.Controllers.api
{
    public class ProductVariantApiController : ApiController
    {

        private IProductService _productService;

        private readonly IWorkContext _workContext;
        private IActivityService _activityService;
        private IMessageService _messageService;


        public ProductVariantApiController(IProductService productService, IWorkContext workContext, IActivityService activityService, IMessageService messageService)
        {
            _productService = productService;
            _workContext = workContext;
            _activityService = activityService;
            _messageService = messageService;
        }

       
        [HttpPost]
        public ProductVariantDeleteResult DeleteProductVariant(ProductVariantDeleteRequest request)
        {
            var result = new ProductVariantDeleteResult();
            try
            {
                _productService.DeleteProductVaraint(request.ProductVariantId);


                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message = _messageService.GetResourceValue(Messages.ProductVariantDeleteSuccess, 0);

                //_activityService.InsertActivity("InsertProductVariantAttributeValue", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.Message;
                return result;
            }
        }

        public ProductVariantInfoSaveResult InsertProductVariantInfo(ProductVariantInfoSaveRequest request)
        {
            var result = new ProductVariantInfoSaveResult();


            try
            {

                var productVariantId = _productService.InsertProductVariantInfo(request.ProductId, request.Name,
                     request.Sku, request.Price, request.DisplayOrder, request.StockQuantity, request.PictureId, request.Published);


                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message= _messageService.GetResourceValue(Messages.ProductVariantCreateInfoSuccess, 0);
                result.Id = productVariantId;

                _activityService.InsertActivity("InsertProductVariantInfo", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.Message;
                return result;
            }
        }


        public ProductVariantInfoSaveResult UpdateProductVariantInfo(ProductVariantInfoSaveRequest request)
        {
            var result = new ProductVariantInfoSaveResult();


            try
            {

                _productService.UpdateProductVariantInfo(request.Id, request.Name,
                     request.Sku, request.Price, request.DisplayOrder, request.StockQuantity, request.PictureId, request.Published);


                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message = _messageService.GetResourceValue(Messages.ProductVariantUpdateSuccess, 0);
               

                _activityService.InsertActivity("UpdateProductVariantInfo", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.Message;
                return result;
            }
        }
    }

    public class ProductVariantDeleteRequest
    {
        public int ProductVariantId { get; set; }
    }

    public class ProductVariantDeleteResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
}
